package com.li.controller;

import com.alibaba.fastjson.JSONObject;
import com.li.domain.FileAddress;
import com.li.domain.UploadFile;
import com.li.service.UploadFileService;
import com.li.utlis.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/uploadFile")
public class UploadFileController {

    @Autowired
    private UploadFileService uploadFileService;

    @RequestMapping("/listPageUploadFile")
    public JSONObject listPageUploadFile(UploadFile uploadFile, Integer page, Integer limit){
        List<UploadFile> list=uploadFileService.listPageUploadFile(uploadFile,page,limit);
        Integer count=uploadFileService.listPageUploadFileCount(uploadFile);
        return R.listPage(list,count);
    }

    @GetMapping("/delUploadFile/{id}")
    public JSONObject delUploadFile(@PathVariable("id") Integer id){
        try {
            boolean flag=uploadFileService.delUploadFile(id);
            if (!flag){
                return R.getError(id);
            }
        }catch (Exception e){
            return R.getError(id);
        }
        return R.getSuccess();
    }

    @RequestMapping("/delUploadFiles")
    public JSONObject delUploadFiles(UploadFile uploadFile){
        Integer ids[]=uploadFile.getIds();
        try {
            boolean flag=uploadFileService.delUploadFiles(ids);
            if (!flag){
                return R.getError(ids);
            }
        }catch (Exception e){
            return R.getError(ids);
        }
        return R.getSuccess();
    }

}
